﻿using MyToolkit.Multimedia;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Input;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Youtube_HD.Model;
using Youtube_HD.Services;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=391641

namespace Youtube_HD.View
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            Windows.Phone.UI.Input.HardwareButtons.BackPressed += _HardwareButtons_BackPressed;
            this.NavigationCacheMode = NavigationCacheMode.Required;
          
        }

        private void _HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
        {
            Frame frame = Window.Current.Content as Frame;
            if (frame == null)
            {
                return;
            }
            if (frame.CanGoBack)
            {
                frame.GoBack();
                e.Handled = true;
            }
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.
        /// This parameter is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // TODO: Prepare page for display here.

            // TODO: If your application contains multiple pages, ensure that you are
            // handling the hardware Back button by registering for the
            // Windows.Phone.UI.Input.HardwareButtons.BackPressed event.
            // If you are using the NavigationHelper provided by some templates,
            // this event is handled for you.
        }
        private IHomeViewModel homeViewModel
        {
            get { return (IHomeViewModel)DataContext; }
        }
        private async void Videos_ItemClick(object sender, ItemClickEventArgs e)
        {
            homeViewModel.LoadingComplete = true;
            var getDat = (YoutubeVideo)e.ClickedItem;
            homeViewModel.LoadingComplete = false;
            Frame fm = Window.Current.Content as Frame;
            fm.Navigate(typeof(DetailPage), getDat);
        }

        private void SelectItemData(object sender, HubSectionHeaderClickEventArgs e)
        {
            var getSection = e.Section.Name;
            switch (getSection)
            {
                case "search":
                    Frame fm = Window.Current.Content as Frame;
                    fm.Navigate(typeof(SearchPage));
                    break;


            }
        }

        private async void nextPageSearch(object sender, KeyRoutedEventArgs e)
        {
            if (e.Key == Windows.System.VirtualKey.Enter)
            {
                if (e.Key == Windows.System.VirtualKey.Enter)
                {
                    HubData.Focus(FocusState.Programmatic);
                    TextBox textData = sender as TextBox;
                    if (!string.IsNullOrEmpty(textData.Text.Trim()))
                    {
                        string urlData = "https://gdata.youtube.com/feeds/api/videos?q=" + WebUtility.UrlEncode(textData.Text.Trim()) + "&v=2";
                        homeViewModel.GetSearchAsync(urlData);
                    }
                    else
                    {
                        new MessageDialog("Please input to search").ShowAsync();
                    }

                }
            }

        }
    }
}
